/**
 * Created by zqk on 2018/08/15.
 */
$(document).ready(function () {
    var iProjectId=addressUri.getParam('iProjectId');
    var iTestId=addressUri.getParam('iTestId');
    var testCaseList= $('#testCaseList');
    testCaseList.datagrid({
        url:moduleUrl+'test/getProductTestCases',
        queryParams: {
            iProjectId:iProjectId,
            iTestId:iTestId,
        },
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        pageList: [20,50,100,200],
        idField: 'iId',
        singleSelect:false,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sCode',title:'编号',width:100},
            {field:'sModule',title:'模块',width:120},
            {field:'sSubmodule',title:'子模块',width:150},
            {field:'sFunctionPoint',title:'功能点',width:185},
        ]],
        onLoadSuccess:function () {
            $.ajax({
                url:moduleUrl+'test/getCheckedCases',
                data:{
                    iTestId:iTestId,
                },
                type:'POST',
                dataType:'json',
                success:function (data) {
                    for(x in data){
                        testCaseList.datagrid('selectRecord',data[x]);
                    }
                },
                error:function (data) {
                    console.log(data);
                }
            });
        }
    });

    $('#bt_list_search').click(function(){
        var sModule = $('#sModule').val();//获取input值
        testCaseList.datagrid('load',{sModule:sModule,iProjectId:iProjectId,iTestId:iTestId});
    });

    $('#bt_modal_confirm').click(function () {
        myWindow.initPopUp({title: '导入测试用例', text: '确认导入测试用例吗?'},function () {
            var selections = testCaseList.datagrid('getSelections');
            var iIds = [];
            for(k in selections ){
                iIds.push(selections[k].iId)
            }

            var data = {
                'iProjectId':iProjectId,
                'iTestId':iTestId,
                'iIds':iIds
            }
            $.ajax({
                url:moduleUrl+'test/addMultiCase',
                dataType: "JSON",
                data:data,
                type:'POST',
                success:function (data) {
                    window.top.xhTop.showSuccess(
                        data.msg
                    );
                    window.top.xhTop.closeModal();
                    window.top.xhTop.getActiveFrame().caseList.datagrid('reload');
                },
                error:function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        })

    });

    $('#bt_modal_close').click(function () {
        window.top.xhTop.closeModal();
    })

});